/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Edf;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
 *
 * @author Gebruiker
 */public class EDFRead {
    private File file;
    public EDFRead(String path) {
        file = new File(path);
    }
    public double[] getData() {
        
        
        byte dataVersion[] = new byte[8];
        byte patientID[] = new byte[80];
        byte recordingID[] = new byte[80];
        byte startdate[] = new byte[8];
        byte starttime[] = new byte[8];
        byte bytesInHeader[] = new byte[8];
        byte firstReserved [] = new byte [44];
        byte numberOfRecords [] = new byte[8]; 
        //new Complex(8000, 0)
        byte recordDuration [] = new byte[8];
        byte signalsPerRecord [] = new byte[4];
        int ns = 0;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        
        try {
          fis = new FileInputStream(file);

          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
          
          
         //First 8 bytes represent the data version
          for (int i=0;i<dataVersion.length;i++) {
              dataVersion[i] = dis.readByte();
          }
          
          for (int i=0;i<patientID.length;i++) {
              patientID[i] = (dis.readByte());
          }
          
          for (int i=0;i<recordingID.length;i++) {
              recordingID[i] = (dis.readByte());
          }
          
          for (int i=0;i<startdate.length;i++) {
              startdate[i] = (dis.readByte());
          }
          
          for (int i=0;i<starttime.length;i++) {
              starttime[i] = (dis.readByte());
          }
          
          for (int i=0;i<bytesInHeader.length;i++) {
              bytesInHeader[i] = (dis.readByte());
          }
          
          for (int i=0;i<firstReserved.length;i++) {
              firstReserved[i] = (dis.readByte());
          }
          
          for (int i=0;i<numberOfRecords.length;i++) {
              numberOfRecords[i] = (dis.readByte());
          }
          
          for (int i=0;i<recordDuration.length;i++) {
              recordDuration[i] = (dis.readByte());
          }
          
          for (int i=0;i<signalsPerRecord.length;i++) {
              signalsPerRecord[i] = (dis.readByte());
          }
          ns = Integer.parseInt(new String(signalsPerRecord).trim());
          
          
          byte labels[] = new byte[ns*16];
          for (int i = 0; i < labels.length; i++) {
              labels[i] = dis.readByte();
          }
          
          byte transducerType[] = new byte[ns*80];
          for (int i = 0; i < transducerType.length; i++) {
              transducerType[i] = dis.readByte();
          }
          
          byte physDimension[] = new byte[ns*8];
          for (int i = 0; i < physDimension.length; i++) {
              physDimension[i] = dis.readByte();
          }
          
          byte physMinimum[] = new byte[ns*8];
          for (int i = 0; i < physMinimum.length; i++) {
              physMinimum[i] = dis.readByte();
          }
          
          byte physMaximum[] = new byte[ns*8];
          for (int i = 0; i < physMaximum.length; i++) {
              physMaximum[i] = dis.readByte();
          }
          
          byte digitalMinimum[] = new byte[ns*8];
          for (int i = 0; i < digitalMinimum.length; i++) {
              digitalMinimum[i] = dis.readByte();
          }
          
          byte digitalMaximum[] = new byte[ns*8];
          for (int i = 0; i < digitalMaximum.length; i++) {
              digitalMaximum[i] = dis.readByte();
          }
          
          byte preFiltering[] = new byte[ns*80];
          for (int i = 0; i < preFiltering.length; i++) {
              preFiltering[i] = dis.readByte();
          }
          
          byte samplesPerRecord[] = new byte[ns*8];
          for (int i = 0; i < samplesPerRecord.length; i++) {
              samplesPerRecord[i] = dis.readByte();
          }
          
          byte reservedSet[] = new byte[ns*32];
          for (int i = 0; i < reservedSet.length; i++) {
              reservedSet[i] = dis.readByte();
          }
          //array contains amount of samples in each record for each Signal. (same order as labels.)
          
          String [] samplesPerRecordPerSignal = new String(samplesPerRecord).trim().replaceAll(" +", "-").split("-");
          
          int nr = Integer.parseInt(new String(numberOfRecords).trim());
          /*
          System.out.println("Version: " + new String(dataVersion));
          System.out.println("startdate: " + new String(startdate));
          System.out.println("starttime: " + new String(starttime));
          System.out.println("First reserved set: " + new String(firstReserved));
          System.out.println("Duration of record in seconds: " + new String(recordDuration));
          System.out.println("bytes in header: " + new String(bytesInHeader));
          System.out.println("Number of records: " + new String(numberOfRecords));
          System.out.println("Record duration: " + new String(recordDuration));
          System.out.println("Signals per record: " + ns);
          System.out.println("labels: " + new String(labels));
          System.out.println("tranducerType: " + new String(transducerType));
          System.out.println("physical dimension: " + new String(physDimension));
          System.out.println("physical minimum: " + new String(physMinimum));
          System.out.println("physical maximum: " + new String(physMaximum));
          System.out.println("Digital minimum: " + new String(digitalMinimum));
          System.out.println("Digital maximum: " + new String(digitalMaximum));
          System.out.println("Samples per record: " + new String(samplesPerRecord));
          System.out.println("Reserved set: " + new String(reservedSet));
          //System.out.println("DATA(size " + data.length +"):");
          */
          /*for (int i = 0; i < nr; i++) {
              for (int j = 0; j < ns; j++) {
                  System.out.print(data[i][j] + "\t");
              }
              System.out.println("");
          }*/
          fis.close();
          bis.close();
          dis.close();
          return new double[]{Double.parseDouble(new String(recordDuration)), Double.parseDouble(samplesPerRecordPerSignal[0])};
          
          
          
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
            
            //streams are normally closed here
        }
        return new double[]{0};
    }
}
